using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System.Collections.Generic;

namespace IQIGame.Onigao.GamePlay
{
    public class UIFloatNoticeItem : UIBaseWindow
    {
        #region Template Generate,don't modify
        protected class UIB_UIFloatNoticeItem
        {
            #region ObjectBinding Generate
            public UnityEngine.GameObject info { protected set; get; }
            public UnityEngine.GameObject content { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<UnityEngine.GameObject>("info", out var __tbv0);
                this.info = __tbv0;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("content", out var __tbv1);
                this.content = __tbv1;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region Window Config
        /// <summary>
        /// 静态配置
        /// </summary>
        private static UICreateConfig _createConfig = new()
        {
            prefabName = nameof(UIFloatNoticeItem),
            layer = EUILayer.Normal,
        };

        /// <summary>
        /// 创建UI的配置
        /// </summary>
        public override IUICreateConfig createConfig => _createConfig;
        #endregion Window Config

        #region fields
        private UIItemPool<UIN_FloatNoticeItem> poolUnits;
        private List<(int, int)> datas = new(2);
        private bool isPlayingNodeShowAnimation = false;
        private int nodeShowing = 0;
        #endregion fields

        #region properties

        protected UIB_UIFloatNoticeItem ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIFloatNoticeItem();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.isUpdate = true;

            this.poolUnits = this.InitPool<UIN_FloatNoticeItem>(this.ui.info);
        }

        protected override void OnShow(UIBaseData data)
        {
            var ud = (UD_FloatNoticeItem)data;
            this.AddOrMergeBasicItem(ud.itemCid, ud.itemNum);
            this.ShowNode();
        }

        protected override void OnHide()
        {

        }


        /// <summary>
        /// 添加UGUI侦听
        /// </summary>
        protected override void AddUGUIListener()
        {

        }

        /// <summary>
        /// 添加全局侦听（MsgDispatcher）
        /// </summary>
        protected override void AddMsgListeners()
        {

        }

        protected override void OnDispose()
        {
            this.ClearData();
        }

        #region methonds

        private void AddOrMergeBasicItem(int itemCid, int itemNum)
        {
            this.datas.Add((itemCid, itemNum));
        }

        private void ClearData()
        {
            this.datas.Clear();
        }

        private void ShowNode()
        {
            if (!this.IsNextNode())
            {
                return;
            }
            this.isPlayingNodeShowAnimation = true;
            this.nodeShowing++;

            UIN_FloatNoticeItem node = this.poolUnits.GetOne(false);
            (int itemCid, int itemNum) = GetAndRemoveFirst();
            node.Show(itemCid, itemNum, this.RebackNode, this.OnNodeAnimationShowEnd);
        }

        private (int, int) GetAndRemoveFirst()
        {
            if (this.datas.Count > 0)
            {
                var result = this.datas[0];
                this.datas.Remove(result);
                return result;
            }
            return default;
        }

        private bool IsNextNode()
        {
            return !this.isPlayingNodeShowAnimation && this.nodeShowing < APIUIConst.MaxShowFloatNoticeItem;
        }

        private void RebackNode(UIN_FloatNoticeItem node)
        {
            this.poolUnits.HideOne(node);

            this.nodeShowing--;
            this.Judge();
        }

        private void OnNodeAnimationShowEnd()
        {
            this.isPlayingNodeShowAnimation = false;
            this.Judge();
        }

        private void Judge()
        {
            if (this.datas.Count > 0)
                this.ShowNode();
            else if (this.nodeShowing == 0)
                this.Close();
        }
        #endregion methonds
    }
}
